package com.atguigu.guli.serviceedu.controller;


import com.alibaba.excel.EasyExcel;
import com.atguigu.guli.serviceedu.entity.ExcelSubjectData;
import com.atguigu.guli.serviceedu.entity.vo.SubjectNestedVo;
import com.atguigu.guli.serviceedu.listener.SubjectExcelListener;
import com.atguigu.guli.serviceedu.service.SubjectService;
import com.atguigu.servicebase.exception.ServiceException;
import com.atguigu.servicebase.result.R;
import com.atguigu.servicebase.result.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(tags = "课程分类管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/edu/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    //Excel批量导入
    @ApiOperation(value = "Excel批量导入")
    @PostMapping("import")
    public R SubjectImport(MultipartFile file) {
        //1 获取上传的excel文件 MultipartFile
        //返回错误提示信息
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            throw new ServiceException(ResponseEnum.FILE_PARSING_ERROR);
        }
        //判断返回集合是否为空
        return R.ok();
    }

    //课程分类列表
    @ApiOperation(value = "课程分类列表")
    @GetMapping
    public R SubjectList() {
        List<SubjectNestedVo> subjectNestedVoList = subjectService.nestedList();
        return R.ok().data("items", subjectNestedVoList);
    }


}
